package cn.sytton.taffecloud.service.system.controller;

import cn.sytton.taffecloud.common.base.doc.Api;
import cn.sytton.taffecloud.common.base.doc.ApiImplicitParam;
import cn.sytton.taffecloud.common.base.doc.ApiOperation;
import cn.sytton.taffecloud.common.base.doc.ParamType;
import cn.sytton.taffecloud.common.db.model.PageParam;
import cn.sytton.taffecloud.common.excel.annotation.ExportExcel;
import cn.sytton.taffecloud.service.system.entity.UserEntity;
import cn.sytton.taffecloud.service.system.service.IUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 用户控制层
 *
 * @author skyrock
 */
@Api(tags = "用户")
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    @ApiOperation("查询分页用户列表")
    @ApiImplicitParam(name = "pp", value = "分页参数对象", paramType = ParamType.query, dataTypeClass = PageParam.class)
    @ExportExcel
    @GetMapping
    public PageInfo<UserEntity> findListPage(PageParam pp) {
        return userService.findListPage(pp);
    }

    @ApiOperation("查询单个用户")
    @ApiImplicitParam(name = "id", value = "用户ID", paramType = ParamType.path)
    @GetMapping("/{id}")
    public UserEntity findById(@PathVariable String id) {
        return userService.findById(id);
    }

    @ApiOperation("添加用户")
    @ApiImplicitParam(name = "entity", value = "用户数据对象", paramType = ParamType.body, dataTypeClass = UserEntity.class)
    @PostMapping
    public void create(@RequestBody @Validated UserEntity entity) {
        userService.create(entity);
    }

    @ApiOperation("更新用户")
    @ApiImplicitParam(name = "entity", value = "用户数据对象", paramType = ParamType.body, dataTypeClass = UserEntity.class)
    @PostMapping("/update")
    public void update(@RequestBody @Validated UserEntity entity) {
        userService.update(entity);
    }

    @ApiOperation("删除用户")
    @ApiImplicitParam(name = "ids", value = "用户ID数组", paramType = ParamType.body, dataTypeClass = String[].class)
    @PostMapping("/delete")
    public void delete(@RequestBody String[] ids) {
        userService.delete(ids);
    }

    @ApiOperation("修改用户密码")
    @ApiImplicitParam(name = "entity", value = "用户数据对象", paramType = ParamType.body, dataTypeClass = UserEntity.class)
    @PostMapping("/pwd/update")
    public void updatePwd(@RequestBody UserEntity entity) {
        userService.updatePwd(entity);
    }

    @ApiOperation("修改用户状态")
    @ApiImplicitParam(name = "entity", value = "用户数据对象", paramType = ParamType.body, dataTypeClass = UserEntity.class)
    @PostMapping("/status/update")
    public void updateUserStatus(@RequestBody UserEntity entity) {
        userService.updateUserStatus(entity);
    }

    @ApiOperation("修改我的密码")
    @ApiImplicitParam(name = "pwdData", value = "oldPwd:旧密码, newPwd:新密码", paramType = ParamType.body, dataTypeClass = Map.class)
    @PostMapping("/my-pwd/update")
    public void updateMyPwd(@RequestBody Map<String, String> pwdData) {
        userService.updateMyPwd(pwdData.get("oldPwd"), pwdData.get("newPwd"));
    }


}
